//
//  TPPermissionPhotoTools.m
//  Pods
//
//  Created by caixun on 16/1/22.
//
//


#import "MPTAuthorizationPhotoTools.h"
#import <AssetsLibrary/AssetsLibrary.h>


@interface MPTAuthorizationPhotoTools ()
{
    AuthorizationHandler _completion;
}


@end


@implementation MPTAuthorizationPhotoTools

+ (instancetype)sharedInstance
{
    static MPTAuthorizationPhotoTools *_instance = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        _instance = [[MPTAuthorizationPhotoTools alloc] init];
    });
    
    return _instance;
}

- (MPE_AuthorizationStatus)authorizationStatus
{
    ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
    
    switch (status)
    {
        case ALAuthorizationStatusAuthorized:
            return MPE_AuthorizationAuthorized;
        case ALAuthorizationStatusNotDetermined:
            return MPE_AuthorizationNotDetermined;
        case ALAuthorizationStatusRestricted:
        case ALAuthorizationStatusDenied:
            return MPE_AuthorizationDenied;
    }
}

- (void)authorize:(AuthorizationHandler)completion
{
    ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
    switch (status)
    {
        case ALAuthorizationStatusAuthorized:
            if (completion)
            {
                completion(MPE_AuthorizationAuthorized, nil);
            }
            break;
        case ALAuthorizationStatusNotDetermined:
        {
            _completion = completion;
            [self actuallyAuthorize];
        } break;
        case ALAuthorizationStatusRestricted:
        case ALAuthorizationStatusDenied: {
            if (completion)
            {
                completion(MPE_AuthorizationDenied, nil);
            }
        } break;
    }
}

- (MPE_AuthorizationType)permissionType
{
    return MPE_AuthorizationPhotos;
}

- (void)actuallyAuthorize
{
    ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
    
    switch (status)
    {
        case ALAuthorizationStatusAuthorized:
            if (_completion)
            {
                _completion(MPE_AuthorizationAuthorized, nil);
            }
            break;
        case ALAuthorizationStatusNotDetermined:
        {
            ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
            
            [library enumerateGroupsWithTypes:ALAssetsGroupAll
                                   usingBlock:^(ALAssetsGroup *assetGroup, BOOL *stop) {
                                       if (*stop)
                                       {
                                           if (_completion)
                                           {
                                               _completion(MPE_AuthorizationAuthorized, nil);
                                           }
                                       }
                                       else
                                       {
                                           *stop = YES;
                                       }
                                   }
                                 failureBlock:^(NSError *error) {
                                     if (_completion)
                                     {
                                         _completion(MPE_AuthorizationDenied, error);
                                     }
                                 }];
        } break;
        case ALAuthorizationStatusRestricted:
        case ALAuthorizationStatusDenied:
        {
            if (_completion)
            {
                _completion(MPE_AuthorizationDenied, nil);
            }
        } break;
    }
}


@end
